#include "mainwindow.h"
#include "ui_mainwindow.h"

using namespace std;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_ok_clicked(bool checked)
{
    QString  static_ip;
    QString  static_gateway;
    QString  static_netmask;
    QString  static_dns1;
    QString  static_dns2;
    QString  set_ip_cmd;
    QString  set_num;
    QString  sub_num;

    static_ip = ui->lineEdit_ip->text();
    static_gateway = ui->lineEdit_gateway->text();
    static_netmask = ui->lineEdit_netmask->text();
    static_dns1 = ui->lineEdit_dns1->text();
    static_dns2 = ui->lineEdit_dns2->text();
    sub_num = ui->lineEdit_sub->text();
    set_num = ui->lineEdit_num->text();

    if (is_valid_ip(static_ip))
    {
        QString result = static_ip + " false";
        ui->lineEdit_result->setPlaceholderText(result);
        return;
    }

    if (is_valid_ip(static_gateway))
    {
        QString result = static_gateway + " false";
        ui->lineEdit_result->setPlaceholderText(result);
        return;
    }

    if (is_valid_ip(static_netmask))
    {
        QString result = static_netmask + " false";
        ui->lineEdit_result->setPlaceholderText(result);
        return;
    }

    if (is_valid_ip(static_dns1))
    {
        QString result = static_dns1 + " false";
        ui->lineEdit_result->setPlaceholderText(result);
        return;
    }



    int num = set_num.toInt();

    /*  start 为 1--6 end 为 01--12   */
    QString start = sub_num.mid(3, 1);
    QString end = sub_num.mid(5, 2);
    int tmp_end = end.toInt();
    int tmp_start = start.toInt();


    for (int i = 0; i < num; i++)
    {
        /*
        *   解 sub1-01
        * 按字符数量拆分字符串
        * start = str.mid(3, 1) 取出1
        * end = str.mid(5, 2) 取出01
        * 转换城int 需要判断01
        * 自增
        * 需要与10做比较 转换成字符串时判断是否添加字符'0'
        *
        *
        *
        *   解sub1-10
        * 按字符数量拆分字符串
        * start = str.min(3, 1) 取出1
        * end = str.min(5, 2) 取出10
        * 转成int 自增
        * 与12做比较 如大雨12 则start + 1 end 从01记
        *
        */


        if (tmp_end > 12)
        {
            tmp_start++;
            tmp_end = tmp_end - 12;
        }
        start = QString::number(tmp_start);

        end = QString::number(tmp_end);
        if (tmp_end < 10)
        {
            end = '0' + end;
        }

        sub_num = "sub" + start + '-' + end;
        ui->lineEdit->setPlaceholderText(sub_num);
        ui->lineEdit_2->setPlaceholderText(start);
        ui->label_display->setText(static_ip);
        QString adb_cmd = "adb -s " + sub_num + " shell armnetcfg -i ";
        set_ip_cmd = adb_cmd + static_ip + " -m " + static_netmask + " -g " +static_gateway + " -d1 " + static_dns1 + " -d2 " + static_dns2;
        ui->display_adb->setPlaceholderText(set_ip_cmd);

        QString str_tail = static_ip.section('.', 3, 3);    // 取第3个'.'后字符(一共只有3个'.')
        QString str_head = static_ip.section('.', 0, 2);    // 取从第0个'.'到第2个点后字符第三个'.'之前的所有字符
        int tmp_tail = str_tail.toInt();    // QString 转 int

        str_tail = QString::number(tmp_tail);    //int 转 QString
        static_ip = str_head + "." + str_tail;

        ui->lineEdit_tmp->setPlaceholderText(str_tail);
        ui->lineEdit_tmp1->setPlaceholderText(str_head);

        char*  ch;
        QByteArray ba = set_ip_cmd.toLatin1(); // must
        ch = ba.data();
        system(ch);

        tmp_tail++;
        tmp_end ++;
    }

    ui->lineEdit_result->setPlaceholderText("successed");
}

void MainWindow::on_pushButton_cancel_clicked(bool checked)
{
    exit(0);
}
